package com.ybkj.daijia.server.service.impl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.config.RentApi;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.model.InforMation;
import com.ybkj.daijia.server.service.InforMationService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

@Service
public class DefaultInforMationService implements InforMationService {

    @Autowired
    private RentApi rentApi;  //声明rent业务
    @Autowired
    private SettingUtils settingUtils;


    @Override
    public Page<InforMation> selectInforMation(String nameOrPhone,
        Integer page, Integer rows, Integer isPass) {

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());
        Map<String, String> kv = new HashMap<String, String>();
        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL()).append(rentApi.SELECT_ALL_INFOMATION)
            .append("?appkey={appkey}");
        if (StringUtils.isNotBlank(nameOrPhone)) {
            kv.put("nameOrPhone", nameOrPhone);
            builder.append("&nameOrPhone={nameOrPhone}");
        }

        page = Pager.getPage();
        rows = Pager.getLimit();  //每页的条数
        if (isPass != null) {
            kv.put("isPass", String.valueOf(isPass));
            builder.append("&isPass={isPass}");
        }
        if (rows != null) {
            kv.put("rows", String.valueOf(rows));
            builder.append("&rows={rows}");
        }
        if (page != null) {
            kv.put("page", String.valueOf(page + 1));
            builder.append("&page={page}");
        }
        String result = rentApi.get(builder.toString(), kv, settingUtils.get());
        List<InforMation> information = new ArrayList<InforMation>();
        long total = 0;
        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");

            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                total = data.getLong("total");
                JSONArray jsonArray = data.getJSONArray("rows");
                Gson gson = new GsonBuilder().create();
                information = gson
                    .fromJson(jsonArray.toString(), new TypeToken<List<InforMation>>() {
                    }.getType());

            }
        }
        return new PageImpl<InforMation>(information, pageable, total);
    }


    @Override
    public InforMation selectById(Long id) {

        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL()).append(rentApi.SELECT_INFORMATION_BY_ID)
            .append("?id={id}&appkey={appkey}");

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("id", String.valueOf(id));
        String result = rentApi.get(builder.toString(), kv, settingUtils.get());

        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");



                /*Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();*/
                Gson gson = new GsonBuilder().create();
                InforMation inforMation = gson.fromJson(data.toString(), InforMation.class);

                return inforMation;
            }
        }
        return null;
    }

    @Override
    public Map<String, Object> updateInforMation(Long id,
        Integer idcardpositiveresult, Integer idcardbackresult,
        Integer driverresult, String idcardpositiveremarks,
        String idcardbackremarks, String driverremarks) {
		
	/*	  StringBuilder builder=new StringBuilder();
		  builder.append(rentApi.getApiBaseURL()).append(rentApi.UPDATE_INFOMATION).append("?id={id}");*/

        Map<String, Object> map = new HashMap<String, Object>();

        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();

        kv.add("id", String.valueOf(id));
        kv.add("idcardpositiveresult", String.valueOf(idcardpositiveresult));
        kv.add("idcardbackresult", String.valueOf(idcardbackresult));
        kv.add("driverresult", String.valueOf(driverresult));
        kv.add("idcardpositiveremarks", idcardpositiveremarks);
        kv.add("idcardbackremarks", idcardbackremarks);
        kv.add("driverremarks", driverremarks);
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.UPDATE_INFOMATION, kv,
                settingUtils.get());

        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "修改成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }


    //插入
    @Override
    public Map<String, Object> insertInforMation(String phone, String name, String idcard,
        String drivernumber, String photourl, Integer registrationtime,
        String reviewer, String idcardpositivephoto,
        String idcardbackphoto, String driverphoto,
        Integer idcardpositiveresult, Integer idcardbackresult,
        Integer driverresult, String idcardpositiveremarks,
        String idcardbackremarks, String driverremarks) {
		
		/*StringBuilder builder=new StringBuilder();
		builder.append(rentApi.getApiBaseURL()).append(rentApi.INSERT_INFOMATION).append("?id={id}&appkey={appkey}");*/
		
/*		String result = rentApi.getApiBaseURL() + rentApi.INSERT_INFOMATION + "?appKey={appKey}&appId={appId}&appId={appId}&appId={appId}&appId={appId}"
				+ "&appId={appId}&appId={appId}&appId={appId}&appId={appId}&appId={appId}&appId={appId}&appId={appId}&appId={appId}&appId={appId}"
				+ "&appId={appId}&appId={appId}"; */
        List<InforMation> list = new ArrayList<InforMation>();
        Map<String, String> kv = new HashMap<String, String>();
        kv.put("phone", phone);
        kv.put("name", name);
        kv.put("idcard", idcard);
        kv.put("drivernumber", drivernumber);
        kv.put("photourl", photourl);
        kv.put("registrationtime", String.valueOf(registrationtime));
        kv.put("reviewer", reviewer);
        kv.put("idcardpositivephoto", idcardpositivephoto);
        kv.put("idcardbackphoto", idcardbackphoto);
        kv.put("driverphoto", driverphoto);
        kv.put("idcardpositiveresult", String.valueOf(idcardpositiveresult));
        kv.put("idcardbackresult", String.valueOf(idcardbackresult));
        kv.put("driverresult", String.valueOf(driverresult));
        kv.put("idcardpositiveremarks", idcardpositiveremarks);
        kv.put("idcardbackremarks", idcardbackremarks);
        kv.put("driverremarks", driverremarks);
        String result = rentApi
            .get(rentApi.getApiBaseURL() + rentApi.INSERT_INFOMATION, kv, settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        Map<String, Object> map = new HashMap<String, Object>();
        if (code == 0) {
            map.put("message", "创建成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }


    //批量通过
    @Override
    public Map<String, Object> batchpass(
        String size) {
//		 List<InforMation> list = new ArrayList<InforMation>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("size", size);
        Map<String, Object> map = new HashMap<String, Object>();
        String result = rentApi.postMultiValueMap(rentApi.getApiBaseURL() + RentApi.BATCH_PASS, kv,
            settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "修改成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;


    }


}
